# -*- coding: utf-8 -*-
import TerminalController
import os
import sys
import MySQLdb
import time
import threading
from string import maketrans
from progressbar import ProgressBar

#Going to monitor students

db = MySQLdb.connect(host="localhost",port=3306,user="root",passwd="",db="mmds")
cursor = db.cursor()
logout1 = 'insert into logout select * from temp1 where username not in (select username from temp2)'
sql_mal1 = 'select distinct temp1.username,temp1.ipaddr from temp1,temp2 where temp1.username=temp2.username and temp1.ipaddr!=temp2.ipaddr'
sql_mal2 = 'select temp4.username,temp4.ipaddr,temp3.username,temp3.ipaddr from temp3,temp4 where temp3.username!=temp4.username and temp3.ipaddr=temp4.ipaddr'
sql_mal3 = 'select distinct temp1.username,temp1.ipaddr,temp2.username,temp2.ipaddr from temp1,temp2 where temp1.username!=temp2.username and temp1.ipaddr=temp2.ipaddr'


a = [ ]
falert=0
counter = 10

sec = 0

def initial():
	term = TerminalController.TerminalController()
	print term.render('\n\n\n${YELLOW}Welcome to MMDS - Version 0.2 ${NORMAL}')
	time.sleep(0.05)


	print "\nInitializing MMDS!........."
	p = ProgressBar()
	for i in range(101):
		p.render(i, 'step %s' % i)
		time.sleep(0.05)



	print "\n\n\nInitializing all MMDS Componenets!......."
	p = ProgressBar('green', width=20, block="▣", empty="□")
	for i in range(101):
		p.render(i, 'step %s\nLoading : MMDS Components ' % i)
		time.sleep(0.05)

	print "\nSuccessfully Loaded!.... "


def ipft(st):
	in1="()"
	out1="  "
	sd=maketrans(in1,out1)
	as1 = st.translate(sd)
	as1 = as1.strip()
	return as1

def step1():
	os.system("who"+"> usr1")
	sys2 = open('usr1','r')
	a = sys2.readlines()
	b = [0,1,2,3,4]
	for line in xrange(len(a)):
		b = a[line].split()
		b[4] = ipft(b[4])
		cursor.execute('Insert into temp1 values( "%s", "%s", "%s", "%s", "%s")' % ( b[0],b[1],b[2],b[3],b[4]) )
	 

def step2():
	os.system("who"+"> usr2")
	sys2 = open('usr2','r')
	a = sys2.readlines()
	b = [0,1,2,3,4]
	cursor.execute('Truncate temp2')
	for line in xrange(len(a)):
		b = a[line].split()
		b[4] = ipft(b[4])
		cursor.execute('Insert into temp2 values( "%s", "%s", "%s", "%s", "%s")' % ( b[0],b[1],b[2],b[3],b[4]) )
	
	
def proc(counter):
	step1()
	while sec < counter:
		time.sleep(1)
		os.system('clear')
		print "++++++++++++++++++++++++++++++++++++++++++++++++++++"
		print "MMDS Console v0.3\t\tTime : ", time.strftime("%H %M %S %p", time.localtime(time.time()))
		print "++++++++++++++++++++++++++++++++++++++++++++++++++++"
		os.system('who')
		step2()
		step3()
		counter +=1
	
	

def step3():
	cursor.execute(sql_mal1)
	mal1 = cursor.fetchall()
	cursor.execute(sql_mal3)
	mal2 = cursor.fetchall()
	if len(mal1) > 0 :
		falert=1
		alert(falert,mal1)
	if len(mal2) > 0 :
		falert=2
		alert(falert,mal2)
	


def alert(falert,mal):
	
	if falert == 1:
		print "\n===================================================="
		print "Single User ID Logged in Multiple System Found!..."
		print "====================================================\n"
		for row in mal:
			print "\t-----------------------------"
			print "\tUserID : %s \n\tIP Address : %s" % (row[0],row[1])
			print "\t-----------------------------"
		print "====================================================\n"
	else :
		if falert == 2:
			print "\n===================================================="
			print "Multiple User ID Logged in Single System Found!..."
			print "====================================================\n"
			for row in mal:
				print "\t-----------------------------"
				print "\tUserID : %s \n\tIP Address : %s \n\n\tUserID : %s \n\tIP Address : %s" % (row[0],row[1],row[2],row[3])
				print "\t-----------------------------"
			print "====================================================\n"
initial()
step1()
proc(counter)

db.close()
